package com.ruoyi.xds.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.xds.domain.XdsColumn;
import com.ruoyi.xds.service.IXdsColumnService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 栏目管理Controller
 *
 * @author ruoyi
 * @date 2025-08-27
 */
@RestController
@RequestMapping("/system/column")
public class XdsColumnController extends BaseController {
    @Autowired
    private IXdsColumnService xdsColumnService;

    /**
     * 查询栏目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:list')")
    @GetMapping("/list")
    public TableDataInfo list(XdsColumn xdsColumn) {
        startPage();
        List<XdsColumn> list = xdsColumnService.selectXdsColumnList(xdsColumn);
        return getDataTable(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<XdsColumn> depts = xdsColumnService.selectXdsColumnList(new XdsColumn());
        depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return success(depts);
    }

    /**
     * 导出栏目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:export')")
    @Log(title = "栏目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XdsColumn xdsColumn) {
        List<XdsColumn> list = xdsColumnService.selectXdsColumnList(xdsColumn);
        ExcelUtil<XdsColumn> util = new ExcelUtil<XdsColumn>(XdsColumn.class);
        util.exportExcel(response, list, "栏目管理数据");
    }

    /**
     * 获取栏目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:column:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        return success(xdsColumnService.selectXdsColumnByDeptId(deptId));
    }

    /**
     * 新增栏目管理
     */
    @PreAuthorize("@ss.hasPermi('system:column:add')")
    @Log(title = "栏目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XdsColumn xdsColumn) {
        return toAjax(xdsColumnService.insertXdsColumn(xdsColumn));
    }

    /**
     * 修改栏目管理
     */
    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "栏目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XdsColumn xdsColumn) {
        return toAjax(xdsColumnService.updateXdsColumn(xdsColumn));
    }

    /**
     * 删除栏目管理
     */
    @PreAuthorize("@ss.hasPermi('system:column:remove')")
    @Log(title = "栏目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds) {
        return toAjax(xdsColumnService.deleteXdsColumnByDeptIds(deptIds));
    }
}
